package com.echo.neocommunity.controller;

import com.echo.neocommunity.event.EventProducer;
import com.echo.neocommunity.pojo.Event;
import com.echo.neocommunity.pojo.MyPage;
import com.echo.neocommunity.pojo.User;
import com.echo.neocommunity.service.impl.FollowService;
import com.echo.neocommunity.service.impl.UserServiceImpl;
import com.echo.neocommunity.utils.CommunityConstant;
import com.echo.neocommunity.utils.HostHolder;
import com.echo.neocommunity.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
public class FollowController {

    @Autowired
    private FollowService followService;



    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private EventProducer producer;

    @PostMapping("/follow")
    @ResponseBody
    public R follow(int entityType,int entityId){
        User user = hostHolder.getUser();
        followService.followee(user.getId(), entityType,entityId);
        Event event=new Event();
        event.setTopic(CommunityConstant.TOPIC_FOLLOW);
        event.setUserId(user.getId());
        event.setEntityType(entityType);
        event.setEntityId(entityId);
        event.setEntityUserId(entityId);
        producer.fireEvent(event);
        return R.ok();
    }

    @PostMapping("/unfollow")
    @ResponseBody
    public R unFollow(int entityType,int entityId){
        User user = hostHolder.getUser();
        followService.cancelFollowee(user.getId(), entityType,entityId);
        return R.ok();
    }



    @GetMapping("/followees/{userId}")
    public String getFolloweePage(@PathVariable("userId") int userId, Model model,Long current){
        User userById = userService.getUserById(userId);
        if(userById==null){
            throw new RuntimeException("用户不存在！");
        }
        model.addAttribute("user",userById);
        User user = hostHolder.getUser();
        if(current==null)
            current=(long)1;
        MyPage page=new MyPage();
        page.setPath("/followees/"+userById.getId());
        page.setCurrent(current);
        long followeeCount = followService.findFolloweeCount(userById.getId(), CommunityConstant.ENTITY_TYPE_USER);
        page.setTotal(followeeCount);
        page.setPages(followeeCount%5==0?followeeCount/5:followeeCount/5+1);
        Set<Integer> followees = followService.getFollowees(userById.getId(), CommunityConstant.ENTITY_TYPE_USER, Math.toIntExact(page.getCurrent()-1) * 5, 5);
        model.addAttribute("page",page);
        List<Map<String,Object>> list=new ArrayList<>();
        if(followees!=null){
            for (Integer followee : followees) {
                Map<String,Object> map=new HashMap<>();
                User one = userService.getUserById(followee);
                map.put("user",one);
                Date followeeDate = followService.getFolloweesDate(userById.getId(), CommunityConstant.ENTITY_TYPE_USER, followee);
                map.put("followeeDate",followeeDate);
                boolean isI=user.getId()==followee;
                map.put("isI",isI);
                boolean isFollowee=followService.findFolloweeStatus(user.getId(),CommunityConstant.ENTITY_TYPE_USER,followee);
                map.put("isFollowee",isFollowee);
                list.add(map);
            }
        }
        model.addAttribute("followees",list);
        return "/site/followee";
    }

    @GetMapping("/followers/{userId}")
    public String getFollowerPage(@PathVariable("userId") int userId, Model model,Long current){
        User userById = userService.getUserById(userId);
        if(userById==null){
            throw new RuntimeException("用户不存在！");
        }
        User user = hostHolder.getUser();
        if(current==null)
            current=(long)0;
        MyPage page=new MyPage();
        page.setPath("/followers/"+userById.getId());
        page.setCurrent(current);
        long followerCount = followService.findFollowerCount( CommunityConstant.ENTITY_TYPE_USER,userById.getId());
        page.setTotal(followerCount);
        page.setPages(followerCount%5==0?followerCount/5:followerCount/5+1);
        Set<Integer> followers = followService.getFollowers( CommunityConstant.ENTITY_TYPE_USER,userById.getId(), Math.toIntExact(page.getCurrent()) * 5, 5);
        model.addAttribute("page",page);
        model.addAttribute("user",userById);
        List<Map<String,Object>> list=new ArrayList<>();
        if(followers!=null){
            for (Integer follower : followers) {
                Map<String,Object> map=new HashMap<>();
                User one = userService.getUserById(follower);
                map.put("user",one);
                Date followerDate = followService.getFollowersDate(userById.getId(), CommunityConstant.ENTITY_TYPE_USER, follower);
                map.put("followerDate",followerDate);
                boolean isI=user.getId()==follower;
                map.put("isI",isI);
                boolean isFollower=followService.findFollowerStatus(user.getId(),CommunityConstant.ENTITY_TYPE_USER,follower);
                map.put("isFollower",isFollower);
                list.add(map);
            }
        }
        model.addAttribute("followers",list);
        return "/site/follower";
    }






}
